//
//  DatePickerView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/5/24.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "DatePickerView.h"

@interface DatePickerView ()

@end

@implementation DatePickerView
+ (void)setDatePickerViewWithFrame:(CGRect )rect andTitle:(NSString *)title andSuccessBlock:(DatePickerViewBlcok )block{
    DatePickerView *view = [[DatePickerView alloc]initWithFrame:CGRectMake(0,0,APP_WIDTH, APP_HEIGHT) andTitle:title andBlock:block];
    view.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];;
    view.definesPresentationContext = YES;
    view.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:view animated:YES completion:nil];
}
- (instancetype)initWithFrame:(CGRect )rect andTitle:(NSString *)title andBlock:(DatePickerViewBlcok )blcok{
    if ([super init]) {
        [self setupDateKeyPan];
        _mainBlock = blcok;
        _mainTitle.text = title;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        //设置时间格式
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        NSString *dateStr = [formatter  stringFromDate:_datePicker.date];
        _dateString = dateStr;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    
}
- (void)dismiss:(UITapGestureRecognizer *)tap{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)setupDateKeyPan {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss:)];
    [self.view addGestureRecognizer:tap];
    [self.view addSubview:self.baseView];
    [_baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX).offset(150);
        make.centerY.equalTo(self.view.mas_centerY);
        make.width.equalTo(@300);
        make.height.equalTo(@350);
    }];
    [_baseView addSubview:self.mainTitle];
    [_mainTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->_baseView.mas_centerX);
        make.top.equalTo(self->_baseView.mas_top).offset(10);
        make.height.equalTo(@15);
        make.width.equalTo(@120);
    }];
    [_baseView addSubview:self.datePicker];
    
    [_baseView addSubview:self.confirButton];
    [_confirButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0).offset(-10);
        make.height.equalTo(@40);
    }];
}

- (void)dateChange:(UIDatePicker *)datePicker {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //设置时间格式
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *dateStr = [formatter  stringFromDate:datePicker.date];
    _dateString = dateStr;
}
- (UILabel *)mainTitle{
    if (!_mainTitle) {
        UILabel *label = [[UILabel alloc]init];
        label.textAlignment = 1;
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor darkGrayColor];
        _mainTitle = label;
    }
    return _mainTitle;
}
- (UIDatePicker *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        _datePicker.backgroundColor = [UIColor whiteColor];
        _datePicker.frame = CGRectMake(0, 30, 300, 280);
        //设置地区: zh-中国
        _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        //设置日期模式(Displays month, day, and year depending on the locale setting)
        _datePicker.datePickerMode = UIDatePickerModeDate;
        // 设置当前显示时间
        [_datePicker setDate:[NSDate date] animated:YES];
        // 设置显示最大时间（此处为当前时间）
//        [_datePicker setMaximumDate:[NSDate date]];
        //设置时间格式
        //监听DataPicker的滚动
        [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}
- (UIView *)baseView{
    if (!_baseView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 3;
        view.layer.borderColor = [UIColor lightGrayColor].CGColor;
        view.layer.borderWidth = 1;
        view.layer.masksToBounds = YES;
        _baseView = view;
    }
    return _baseView;
}
- (UIButton *)confirButton{
    if (!_confirButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventTouchUpInside];
        _confirButton = button;
    }
    return _confirButton;
}
- (void)confirmClick:(UIButton *)sender{
    if (_mainBlock) {
//        NSArray *array = [_dateString componentsSeparatedByString:@"年"]
        _mainBlock(_dateString);
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
